# check the key words of each line in a dir. 

require "00_common"



module KeywordsStat
  def self.get_all_files(dir)
  	all_files = Common.get_all_files(dir)
  end

  def self.stat_all_files(dir, keyword_in_field)
    keyword_mapping = Hash.new
    keyword_in_field.each do |keyword, field|
      keyword_mapping[keyword] = 0
    end

  	all_files = get_all_files(dir)
  	all_files.each do |file_name|
  	  file_stat = handle_each_file(dir, file_name, keyword_in_field)

      keyword_in_field.each do |keyword, field|
        keyword_mapping[keyword] = keyword_mapping[keyword] + file_stat[keyword]
      end
  	end

  	keyword_mapping
  end

  def self.handle_each_file(dir, file_name, keyword_in_field)
  	file = File.open dir + file_name, "r"

    keyword_mapping = Hash.new
    keyword_in_field.each do |keyword, field|
      keyword_mapping[keyword] = 0
    end

  	begin
      while line = file.readline
        fields = line.split "|"

        keyword_in_field.each do |keyword, field_num|
          if fields[field_num].include? keyword
            keyword_mapping[keyword] = keyword_mapping[keyword] + 1
          end
        end
      end
  	rescue Exception => e
  		err = e.to_s
      if err == "end of file reached"
        # nothing
      else
        puts err
      end
  	end
  	keyword_mapping
  end
end

begin
  puts "facebook_payload_svr: "
  mapping = KeywordsStat.stat_all_files("./facebook_payload_svr/", {"facebook.com" => 1})
  mapping.each do |key, value|
    puts "#{key}:#{value}"
  end
rescue Exception => e
  puts e.to_s	
end